// pages/list3/list3.js
const app = getApp()
var landui = require("../../utils/util.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    domain: app.globalData.domain,
    trueDomain: app.globalData.trueDomain,
    current_nav: 'index',
    list2: [],
    url: '/pages/toilet/detail',
    msg: '上拉加载更多~',
    current_page: 1,
    per_page: 7,
    total_pages: 1,
    module:'',
    param:{}
  },

// 首页接口
/**
 * 今日推荐    https://ynjksh.cn/api/articles?page=1&number=6&isAids=1&type=special
 * 健康专访    https://ynjksh.cn/api/audios?type=hot&include=expert&number=4&isAids=1
 * 视频天地    https://ynjksh.cn/api/videos?page=2&number=2&isAids=1
 * 图文库      https://ynjksh.cn/api/articles?page=1&number=6&type=image&isAids=1
 * 活动专区    https://ynjksh.cn/api/articles?page=1&number=6&isAids=1&type=special
 */
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var module;
    var param;
    var that = this;
    switch(options.type){
        case 'recommend':
           module = 'articles';
           param = { page: that.data.current_page,number:that.data.per_page,isAids:1}
           break;
           case 'img':
           module = 'articles';
        param = { page: that.data.current_page, number: that.data.per_page, type:'image'};
           break;
           //https://ynjksh.cn/api/videos?page=2&number=2&isAids=1
           case 'video':
           module = 'videos';
        param = { page: that.data.current_page, number: that.data.per_page, isAids: 1 };
           break;
          //https://ynjksh.cn/api/articles?page=1&number=6&isAids=1&type=special
          case 'special':
          module = 'articles';
        param = { page: that.data.current_page, number: that.data.per_page, isAids: 1,type:'special' };
          break;
    }
    that.setData({
      module:module,
      param:param,
  
    })
    that.commonRequst(module, param);
    console.log('22' + that.data.list2);
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    //console.log(that.data.current_page);
    //console.log(that.data.total_pages);

    if (that.data.current_page + 1 <= that.data.total_pages) {
      console.log(that.data.module);
      var str = 'param.page';
      landui.common.post({
        module: that.data.module,
        param: that.data.param,
        success: function (res) {
          that.setData({
            list2: that.data.list2.concat(res.data),
            current_page: that.data.current_page+1,
            total_pages: res.meta.pagination.total_pages,
            [str]: that.data.current_page + 1,
          })
        },
        fail: function () { }
      });
    } else {
      that.setData({
        msg: '已经到底啦~'
      })
    }
  },

  // 封装下请求方法
  commonRequst:function(module,param){

    var that = this;
    landui.common.post({
      module: module,
      param: param,
      success: function (res) {
        console.log(res.data);
        if (res.meta.pagination.total_pages == 1) {
          that.msg = '已经到底啦~'
        } else {
          //that.data.current_page = res.meta.pagination.current_page + 1;
          that.data.total_pages = res.meta.pagination.total_pages
        }
        //console.log(that.data.current_page);
        //console.log(that.data.total_pages);
        var str ='param.page';
        that.setData({
          list2: res.data,
          msg: that.msg,
          current_page:that.data.current_page+1,
          total_pages: res.meta.pagination.total_pages,
          [str]: that.data.current_page+1,
        })
      },
      fail: function () {
      }
    })
  }
})